package net.alpenviewer.app;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MergeImage {
	
	static final int beginLat = 20;  // S
	static final int endLat   = 26;  // N
	static final int beginLon = 120; // W  
	static final int endLon   = 123; // E
	static final int resolution = 1200;
	
//	static final int beginLat = 43;  // S
//	static final int endLat   = 48;  // N
//	static final int beginLon = 5; // W  
//	static final int endLon   = 16; // E
//	static final int resolution = 3600;
	
	
	static final String georef = endLat + "-" +beginLat + "-" +endLon + "-" + beginLon;
	static final int step = 1;
	static final String imgPrefix = ".wgs84.png";
	static final File elevationDir    = new File("relief/elevation");
	
	
	public static void main(String[] args) throws IOException {
		
		final BufferedImage img = new BufferedImage(				
				(endLon - beginLon) * resolution + 1,
				(endLat - beginLat) * resolution + 1,
				BufferedImage.TYPE_INT_ARGB);
		
		for (int i=beginLat; i<endLat; i+=step) { // y
		
			for (int j=beginLon; j<endLon; j+=step) { // x
				
				String fname = elevationDir.getPath() + File.separator + (i>=0?"N":"S") + i + (j>=0?"E":"W") + j + imgPrefix;
				
				File f = new File(fname);
				if (f.exists()) {
					System.out.println(fname);	
					BufferedImage tile = ImageIO.read(f);
					Graphics g = img.getGraphics();
					g.drawImage(tile, (j-beginLon)*resolution,                ((endLat-beginLat)-(i-beginLat)- 1)*resolution,
									  (j-beginLon)*resolution + resolution+1, ((endLat-beginLat)-(i-beginLat)- 1)*resolution + resolution + 1,
							          0, 0, 
							          resolution+1, resolution+1, null);
					g.dispose();					
				}
				
			}
			
		}
		
		final File out = new File(elevationDir.getPath() + File.separator + georef + ".merge.png");
		ImageIO.write(img, "png", out);		
			
	}
	
}
